
export function formatTimeAgo(originalTimestamp) {
    const now = new Date();
    const timestampPlus8Hours = new Date(originalTimestamp);
    timestampPlus8Hours.setHours(timestampPlus8Hours.getHours() + 8);

    const timeDifference = now - timestampPlus8Hours;
    const seconds = Math.floor(timeDifference / 1000);
    const minutes = Math.floor(seconds / 60);
    const hours = Math.floor(minutes / 60);
    const days = Math.floor(hours / 24);
    const padZero = (num) => (num < 10 ? `0${num}` : num);

    if (days < 365) {
        if (days == 0) {
            if (hours >= 12) {
                return `${padZero(timestampPlus8Hours.getUTCMonth() + 1)}/${padZero(timestampPlus8Hours.getUTCDate())}`;
            } else if (hours > 0 && hours <= 12) {
                return `${hours}小时前`;
            } else if (minutes >= 1) {
                return `${minutes}分钟前`;
            } else {
                return '刚刚';
            }
        } else if (days == 1) {
            return `昨天 ${timestampPlus8Hours.getHours()}:${('0' + timestampPlus8Hours.getMinutes()).slice(-2)}`;
        } else {
            return `${padZero(timestampPlus8Hours.getUTCMonth() + 1)}/${padZero(timestampPlus8Hours.getUTCDate())}`;
        }
    } else {
        return `${timestampPlus8Hours.getUTCFullYear()}/${padZero(timestampPlus8Hours.getUTCMonth() + 1)}/${padZero(timestampPlus8Hours.getUTCDate())}`;
    }
}
export function formatLocation(location) {
    if (location == null) {
        return '未知';
    }
    location = location.replace(/省|市/g, '');

    return location;
}
export function isImageUrl(url) {
    const dotIndex = url.lastIndexOf(".");
    // 截取路径的扩展名
    const extension = url.substring(dotIndex);
    // 判断扩展名是否是图片扩展名
    const imageExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tif', '.tiff'];
    // console.log(url+'----后缀：'+ extension +'----->  '+imageExtensions.includes(extension))
    return imageExtensions.includes(extension);
}
export function isVideoUrl(url) {
    const dotIndex = url.lastIndexOf(".");
    // 截取路径的扩展名
    const extension = url.substring(dotIndex);
    // 判断扩展名是否是图片扩展名
    let videoExtensions = [".3gp", ".avi", ".flv", ".mkv", ".mov", ".mpg", ".mp4", ".rmvb", ".wmv"];
    // console.log(url+'----后缀：'+ extension +'----->  '+imageExtensions.includes(extension))
    return videoExtensions.includes(extension) && url.startsWith("http");
}

export function getDateTime(){
    // 获取当前时间精确到秒
    var now = new Date();

// 获取各部分时间信息
    var year = now.getFullYear(); // 年份
    var month = now.getMonth() + 1; // 月份（注意JavaScript中月份是从0开始计数的，需要+1）
    var day = now.getDate(); // 日
    var hours = now.getHours(); // 小时
    var minutes = now.getMinutes(); // 分钟
    var seconds = now.getSeconds(); // 秒

// 格式化输出当前时间（例如：2024-02-18 15:30:45）
    return (year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + (hours < 10 ? '0' + hours : hours) + ':' + (minutes < 10 ? '0' + minutes : minutes) + ':' + (seconds < 10 ? '0' + seconds : seconds));

// 或者只获取当前时间戳（毫秒级），然后转换为秒
    // var currentTimeInSeconds = Math.floor(now.getTime() / 1000);
    // console.log(currentTimeInSeconds); // 输出从1970年1月1日0点0分0秒至今的总秒数

}

export function calculateAge(birthDateString) {
    // 将输入的生日字符串转换为Date对象
    var birthDate = new Date(birthDateString);

    // 获取今天的日期
    var today = new Date();

    // 计算年份差异并向下取整（因为可能未过生日）
    var age = today.getFullYear() - birthDate.getFullYear();

    // 检查月份和日期以确保没有过生日
    if (today.getMonth() < birthDate.getMonth() ||
        (today.getMonth() === birthDate.getMonth() && today.getDate() < birthDate.getDate())) {
        age--; // 如果还没过今年的生日，则年龄减一
    }

    return age;
}

